<html>
 <head>
  <meta charset="UTF-8">
 </head>
 <body>
  <h1 data-lake-id="kdxp1" id="kdxp1"><span data-lake-id="u466b3815" id="u466b3815">典型回答</span></h1>
  <p data-lake-id="u54148f08" id="u54148f08"><br></p>
  <p data-lake-id="u41e98911" id="u41e98911"><span data-lake-id="ud56dd8c0" id="ud56dd8c0">一个Spring的Bean从出生到销毁的全过程就是他的整个生命周期，那么经历以下几个阶段：</span></p>
  <p data-lake-id="u211d2e99" id="u211d2e99"><img src="https://cdn.nlark.com/yuque/0/2023/png/5378072/1699966720629-4af99903-18c2-4e0a-9bdf-9674e7aac9cc.png?x-oss-process=image%2Fwatermark%2Ctype_d3F5LW1pY3JvaGVp%2Csize_53%2Ctext_SmF2YSA4IEd1IFA%3D%2Ccolor_FFFFFF%2Cshadow_50%2Ct_80%2Cg_se%2Cx_10%2Cy_10"></p>
  <p data-lake-id="u28d91b24" id="u28d91b24"><br></p>
  <p data-lake-id="u65f4b8aa" id="u65f4b8aa"><span data-lake-id="u3f5c8ba1" id="u3f5c8ba1">​</span><br></p>
  <p data-lake-id="uad0e751b" id="uad0e751b"><strong><span data-lake-id="u147d2efc" id="u147d2efc">整个生命周期可以大致分为3个大的阶段，分别是：</span></strong><strong><span data-lake-id="ubdd2a207" id="ubdd2a207" style="color: #DF2A3F">创建、使用、销毁</span></strong><strong><span data-lake-id="ueb90eb66" id="ueb90eb66">。还可以进一步分为5个小的阶段：</span></strong><strong><u><span data-lake-id="u22893535" id="u22893535">实例化、初始化、注册Destruction回调、Bean的正常使用以及Bean的销毁</span></u></strong><strong><span data-lake-id="u57ad63a7" id="u57ad63a7">。</span></strong></p>
  <p data-lake-id="uc2f57b96" id="uc2f57b96"><strong><span data-lake-id="uf17a6b85" id="uf17a6b85">​</span></strong><br></p>
  <blockquote data-lake-id="u3acab06a" id="u3acab06a">
   <p data-lake-id="u15ecf038" id="u15ecf038"><span data-lake-id="uf9d88e62" id="uf9d88e62">有人把设置属性值这一步单独拿出来了，主要是因为在源码中doCreateBean是先调了populateBean进行属性值的设置，然后再调initializeBean进行各种前置&amp;后置处理。但是其实属性的设置其实就是初始化的一部分。要不然初始化啥呢？</span></p>
   <p data-lake-id="u75109da5" id="u75109da5"><span data-lake-id="u878970f1" id="u878970f1">​</span><br></p>
   <p data-lake-id="u445cbf9c" id="u445cbf9c"><span data-lake-id="u181e61b5" id="u181e61b5">有人也把注册Destruction回调放到销毁这一步了，其实是不对的，其实他不算初始化的一步，也不应该算作销毁的一个过程，他虽然和销毁有关，但是他是在创建的这个生命周期中做的。</span></p>
  </blockquote>
  <p data-lake-id="u0bb80214" id="u0bb80214"><span data-lake-id="u9e58a8b3" id="u9e58a8b3">​</span><br></p>
  <p data-lake-id="ud2563498" id="ud2563498"><span data-lake-id="u19dd8c3d" id="u19dd8c3d">具体到代码方面，可以参考以下这个更加详细的过程介绍，我把具体实现的代码位置列出来了。</span></p>
  <p data-lake-id="u13dbdef8" id="u13dbdef8"><span data-lake-id="uf1562235" id="uf1562235">​</span><br></p>
  <ol list="uf62e5742">
   <li fid="u4ab87b22" data-lake-id="u9ecd07c5" id="u9ecd07c5"><strong><span data-lake-id="u39c7dbb0" id="u39c7dbb0">实例化Bean</span></strong><span data-lake-id="u065914a6" id="u065914a6">：</span></li>
  </ol>
  <ul list="u67ffec0e" data-lake-indent="1">
   <li fid="u273c7a2d" data-lake-id="u8e0dcbd1" id="u8e0dcbd1"><span data-lake-id="u471cf3a4" id="u471cf3a4">Spring容器首先创建Bean实例。</span></li>
   <li fid="u273c7a2d" data-lake-id="uf71a16d6" id="uf71a16d6"><span data-lake-id="ud07bd103" id="ud07bd103">在</span><strong><span data-lake-id="uc5390b78" id="uc5390b78" style="color: #ECAA04">AbstractAutowireCapableBeanFactory</span></strong><span data-lake-id="u3d767945" id="u3d767945">类中的</span><strong><span data-lake-id="u687c9ed8" id="u687c9ed8">createBeanInstance</span></strong><span data-lake-id="u6aab9b22" id="u6aab9b22">方法中实现</span></li>
  </ul>
  <ol list="uf62e5742" start="2">
   <li fid="u4ab87b22" data-lake-id="ub37c9816" id="ub37c9816"><strong><span data-lake-id="u4222df2b" id="u4222df2b">设置属性值</span></strong><span data-lake-id="u55b96d01" id="u55b96d01">：</span></li>
  </ol>
  <ul list="u9b10f6c9" data-lake-indent="1">
   <li fid="u03be6799" data-lake-id="uca906c70" id="uca906c70"><span data-lake-id="u70d1a910" id="u70d1a910">Spring容器注入必要的属性到Bean中。</span></li>
   <li fid="u03be6799" data-lake-id="u53d1afe5" id="u53d1afe5" style="text-align: left"><span data-lake-id="u090c4411" id="u090c4411">在</span><strong><span data-lake-id="u6a295c31" id="u6a295c31" style="color: #ECAA04">AbstractAutowireCapableBeanFactory</span></strong><span data-lake-id="u59e3de2e" id="u59e3de2e">的</span><strong><span data-lake-id="ub5e2b189" id="ub5e2b189">populateBean</span></strong><span data-lake-id="u47e4bf4a" id="u47e4bf4a">方法中处理</span></li>
  </ul>
  <ol list="uf62e5742" start="3">
   <li fid="u4ab87b22" data-lake-id="u0a8ee691" id="u0a8ee691"><strong><span data-lake-id="ua7e2fecf" id="ua7e2fecf">检查Aware</span></strong><span data-lake-id="u2da440cd" id="u2da440cd">：</span></li>
  </ol>
  <ul list="u1381d5fd" data-lake-indent="1">
   <li fid="udc3f9aea" data-lake-id="u2b11d23b" id="u2b11d23b"><span data-lake-id="uf11aa3f0" id="uf11aa3f0">如果Bean实现了BeanNameAware、BeanClassLoaderAware等这些Aware接口，Spring容器会调用它们。</span></li>
   <li fid="udc3f9aea" data-lake-id="u2a4f3b69" id="u2a4f3b69"><span data-lake-id="u46398f71" id="u46398f71">在</span><strong><span data-lake-id="u9d833c04" id="u9d833c04" style="color: #ECAA04">AbstractAutowireCapableBeanFactory</span></strong><span data-lake-id="u6e821bba" id="u6e821bba">的</span><strong><span data-lake-id="u80cc0cca" id="u80cc0cca">initializeBean</span></strong><span data-lake-id="uc79e3c4a" id="uc79e3c4a">方法中调用</span></li>
  </ul>
  <ol list="uf62e5742" start="4">
   <li fid="u4ab87b22" data-lake-id="u1b1b1b43" id="u1b1b1b43"><strong><span data-lake-id="u0ad95945" id="u0ad95945">调用BeanPostProcessor的前置处理方法</span></strong><span data-lake-id="u139a1e3c" id="u139a1e3c">：</span></li>
  </ol>
  <ul list="u492df0cd" data-lake-indent="1">
   <li fid="ub9d551b9" data-lake-id="ub84771f3" id="ub84771f3"><span data-lake-id="uaa24353b" id="uaa24353b">在Bean初始化之前，允许自定义的BeanPostProcessor对Bean实例进行处理，如修改Bean的状态。BeanPostProcessor的postProcessBeforeInitialization方法会在此时被调用。</span></li>
   <li fid="ub9d551b9" data-lake-id="u01be9252" id="u01be9252" style="text-align: left"><span data-lake-id="uf7c63532" id="uf7c63532">由</span><strong><span data-lake-id="uc300b006" id="uc300b006" style="color: #ECAA04">AbstractAutowireCapableBeanFactory</span></strong><span data-lake-id="ud32939ff" id="ud32939ff">的</span><strong><span data-lake-id="ucceea7e6" id="ucceea7e6">applyBeanPostProcessorsBeforeInitialization</span></strong><span data-lake-id="u890f4d7d" id="u890f4d7d">方法执行。</span></li>
  </ul>
  <ol list="uf62e5742" start="5">
   <li fid="u4ab87b22" data-lake-id="u0abd6ef0" id="u0abd6ef0"><strong><span data-lake-id="uc9dd8b99" id="uc9dd8b99">调用InitializingBean的afterPropertiesSet方法</span></strong><span data-lake-id="u51755ac8" id="u51755ac8">：</span></li>
  </ol>
  <ul list="udb9da3a7" data-lake-indent="1">
   <li fid="u90be40f0" data-lake-id="u57b50d16" id="u57b50d16"><span data-lake-id="u5e63c431" id="u5e63c431">提供一个机会，在所有Bean属性设置完成后进行初始化操作。如果Bean实现了InitializingBean接口，afterPropertiesSet方法会被调用。</span></li>
   <li fid="u90be40f0" data-lake-id="u35469e6e" id="u35469e6e" style="text-align: left"><span data-lake-id="uc546c266" id="uc546c266">在</span><strong><span data-lake-id="ud43e48d5" id="ud43e48d5" style="color: #ECAA04">AbstractAutowireCapableBeanFactory</span></strong><span data-lake-id="u2961b2b3" id="u2961b2b3">的</span><strong><span data-lake-id="ua02b376f" id="ua02b376f">invokeInitMethods</span></strong><span data-lake-id="u459925c7" id="u459925c7">方法中调用。</span></li>
  </ul>
  <ol list="uf62e5742" start="6">
   <li fid="u4ab87b22" data-lake-id="u91a9efe6" id="u91a9efe6"><strong><span data-lake-id="u51106689" id="u51106689">调用自定义init-method方法：</span></strong></li>
  </ol>
  <ul list="u296e15c4" data-lake-indent="1">
   <li fid="u90be40f0" data-lake-id="u32bfc6ba" id="u32bfc6ba"><span data-lake-id="uc407130d" id="uc407130d">提供一种配置方式，在XML配置中指定Bean的初始化方法。如果Bean在配置文件中定义了初始化方法，那么该方法会被调用。</span></li>
   <li fid="u90be40f0" data-lake-id="ubc95df31" id="ubc95df31" style="text-align: left"><span data-lake-id="u955c553f" id="u955c553f">在</span><strong><span data-lake-id="u471ba501" id="u471ba501" style="color: #ECAA04">AbstractAutowireCapableBeanFactory</span></strong><span data-lake-id="u9b1a6993" id="u9b1a6993">的</span><strong><span data-lake-id="uac9a14eb" id="uac9a14eb">invokeInitMethods</span></strong><span data-lake-id="u7315ce6b" id="u7315ce6b">方法中调用。</span></li>
  </ul>
  <ol list="uf62e5742" start="7">
   <li fid="u4ab87b22" data-lake-id="u5cfcb965" id="u5cfcb965"><strong><span data-lake-id="uc6a2a541" id="uc6a2a541">调用BeanPostProcessor的后置处理方法</span></strong><span data-lake-id="u1e506a9a" id="u1e506a9a">：</span></li>
  </ol>
  <ul list="ua211dd12" data-lake-indent="1">
   <li fid="uc25a1363" data-lake-id="ue02d54b4" id="ue02d54b4"><span data-lake-id="ub472c887" id="ub472c887">在Bean初始化之后，再次允许BeanPostProcessor对Bean进行处理。BeanPostProcessor的postProcessAfterInitialization方法会在此时被调用。</span></li>
   <li fid="uc25a1363" data-lake-id="ub656b2be" id="ub656b2be" style="text-align: left"><span data-lake-id="u506aa105" id="u506aa105">由</span><strong><span data-lake-id="u041c5568" id="u041c5568" style="color: #ECAA04">AbstractAutowireCapableBeanFactory</span></strong><span data-lake-id="u803e666d" id="u803e666d">的</span><strong><span data-lake-id="u3d4d94b9" id="u3d4d94b9">applyBeanPostProcessorsAfterInitialization</span></strong><span data-lake-id="uc5fdbd5b" id="uc5fdbd5b">方法执行</span></li>
  </ul>
  <ol list="uf62e5742" start="8">
   <li fid="u4ab87b22" data-lake-id="u68ce20ba" id="u68ce20ba"><strong><span data-lake-id="u9f0c1e73" id="u9f0c1e73">注册</span></strong><strong><span data-lake-id="u7679d7af" id="u7679d7af" class="lake-fontsize-12" style="color: rgb(15, 15, 15)">Destruction回调：</span></strong></li>
  </ol>
  <ul list="u2ffafb41" data-lake-indent="1">
   <li fid="uc25a1363" data-lake-id="u01a32e74" id="u01a32e74"><span data-lake-id="uf029d7a0" id="uf029d7a0">如果Bean实现了DisposableBean接口或在Bean定义中指定了自定义的销毁方法，Spring容器会为这些Bean注册一个销毁回调，确保在容器关闭时能够正确地清理资源。</span></li>
   <li fid="uc25a1363" data-lake-id="uf06f0f70" id="uf06f0f70"><span data-lake-id="ue9f2e3c5" id="ue9f2e3c5">在</span><strong><span data-lake-id="u87bcbf32" id="u87bcbf32" style="color: #ECAA04">AbstractAutowireCapableBeanFactory</span></strong><span data-lake-id="ud2f02249" id="ud2f02249">类中的</span><strong><span data-lake-id="u0e052624" id="u0e052624">registerDisposableBeanIfNecessary</span></strong><span data-lake-id="u1e137892" id="u1e137892">方法中实现</span></li>
  </ul>
  <ol list="uf62e5742" start="9">
   <li fid="u4ab87b22" data-lake-id="uda6f5489" id="uda6f5489"><strong><span data-lake-id="u5b62105e" id="u5b62105e">Bean准备就绪</span></strong><span data-lake-id="ubdb032b7" id="ubdb032b7">：</span></li>
  </ol>
  <ul list="uc03486cd" data-lake-indent="1">
   <li fid="u568fef72" data-lake-id="u279bcd6e" id="u279bcd6e"><span data-lake-id="ub6ac600a" id="ub6ac600a">此时，Bean已完全初始化，可以开始处理应用程序的请求了。</span></li>
  </ul>
  <ol list="uf62e5742" start="10">
   <li fid="u4ab87b22" data-lake-id="u1988a73f" id="u1988a73f"><strong><span data-lake-id="u95c6183d" id="u95c6183d">调用DisposableBean的destroy方法</span></strong><span data-lake-id="u4e19bf59" id="u4e19bf59">：</span></li>
  </ol>
  <ul list="u3d4c581a" data-lake-indent="1">
   <li fid="u46d485aa" data-lake-id="u07646e6d" id="u07646e6d"><span data-lake-id="u0fec0d52" id="u0fec0d52">当容器关闭时，如果Bean实现了DisposableBean接口，destroy方法会被调用。</span></li>
   <li fid="u46d485aa" data-lake-id="u127145f5" id="u127145f5"><span data-lake-id="u1d770bcf" id="u1d770bcf">在</span><strong><span data-lake-id="ucfc49fa8" id="ucfc49fa8" style="color: #ECAA04">DisposableBeanAdapter</span></strong><span data-lake-id="u9f20b175" id="u9f20b175">的</span><strong><span data-lake-id="u2f91f4c7" id="u2f91f4c7">destroy</span></strong><span data-lake-id="u9b104d12" id="u9b104d12">方法中实现</span></li>
  </ul>
  <ol list="uf62e5742" start="11">
   <li fid="u4ab87b22" data-lake-id="u623b6df5" id="u623b6df5"><strong><span data-lake-id="u99371a21" id="u99371a21">调用自定义的destory-method</span></strong></li>
  </ol>
  <ul list="uba228905" data-lake-indent="1">
   <li fid="u46d485aa" data-lake-id="uc7513325" id="uc7513325"><span data-lake-id="u1f8f238e" id="u1f8f238e">如果Bean在配置文件中定义了销毁方法，那么该方法会被调用。</span></li>
   <li fid="u46d485aa" data-lake-id="u3a9ddb96" id="u3a9ddb96"><span data-lake-id="ubcf95e3e" id="ubcf95e3e">在</span><strong><span data-lake-id="u6c7ce79a" id="u6c7ce79a" style="color: #ECAA04">DisposableBeanAdapter</span></strong><span data-lake-id="u74479c1f" id="u74479c1f">的</span><strong><span data-lake-id="u1e67570d" id="u1e67570d">destroy</span></strong><span data-lake-id="u1318dce9" id="u1318dce9">方法中实现</span></li>
  </ul>
  <p data-lake-id="u6d8dd6f7" id="u6d8dd6f7"><br></p>
  <p data-lake-id="u7069431a" id="u7069431a"><br></p>
  <p data-lake-id="u0f8641a5" id="u0f8641a5"><span data-lake-id="uac89d8db" id="uac89d8db">可以看到，整个Bean的创建的过程都依赖于</span><strong><span data-lake-id="ue6faf0cc" id="ue6faf0cc" style="color: #ECAA04">AbstractAutowireCapableBeanFactory</span></strong><span data-lake-id="u333636ad" id="u333636ad">这个类，而销毁主要依赖</span><strong><span data-lake-id="uc0d667e4" id="uc0d667e4" style="color: #ECAA04">DisposableBeanAdapter</span></strong><span data-lake-id="u49f50927" id="u49f50927">这个类。</span></p>
  <p data-lake-id="u547076ef" id="u547076ef"><br></p>
  <p data-lake-id="uf489777d" id="uf489777d"><span data-lake-id="ufa2e0c63" id="ufa2e0c63">AbstractAutowireCapableBeanFactory 的入口处，doCreateBean的核心代码如下，其中包含了实例化、设置属性值、初始化Bean以及注册销毁回调的几个核心方法。</span></p>
  <p data-lake-id="uca595d4a" id="uca595d4a"><span data-lake-id="u5fe39aa8" id="u5fe39aa8">​</span><br></p>
  <pre lang="java"><code>
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
			throws BeanCreationException {

		// 实例化bean
		BeanWrapper instanceWrapper = null;
		if (instanceWrapper == null) {
			instanceWrapper = createBeanInstance(beanName, mbd, args);
		}

        // ...

		Object exposedObject = bean;
		try {
            //设置属性值
			populateBean(beanName, mbd, instanceWrapper);
			if (exposedObject != null) {
                //初始化Bean
				exposedObject = initializeBean(beanName, exposedObject, mbd);
			}
		}
		
    	// ...

		// 注册Bean的销毁回调
		try {
			registerDisposableBeanIfNecessary(beanName, bean, mbd);
		}

		return exposedObject;
	}
</code></pre>
  <p data-lake-id="ufdb4e2e1" id="ufdb4e2e1"><br></p>
  <p data-lake-id="uc6e573ff" id="uc6e573ff"><span data-lake-id="uc53bfdfd" id="uc53bfdfd">而</span><strong><span data-lake-id="u216e6887" id="u216e6887" style="color: #ECAA04">AbstractBeanFactory的</span></strong><span data-lake-id="ud7844fa3" id="ud7844fa3">destroy方法中核心内容如下：</span></p>
  <p data-lake-id="ubcf54755" id="ubcf54755"><strong><span data-lake-id="u2d7e21e7" id="u2d7e21e7">​</span></strong><br></p>
  <pre lang="java"><code>
@Override
public void destroy() {
    if (this.invokeDisposableBean) {
            // ...
            ((DisposableBean) bean).destroy();
        }
        // ...
    }

    if (this.destroyMethod != null) {
        invokeCustomDestroyMethod(this.destroyMethod);
    }
    else if (this.destroyMethodName != null) {
        Method methodToCall = determineDestroyMethod();
        if (methodToCall != null) {
            invokeCustomDestroyMethod(methodToCall);
        }
    }
}
</code></pre>
 </body>
</html>